﻿using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public class VerifyCode {
	public static byte[] ToByte() {
		int codeW = 80;
		int codeH = 30;
		int fontSize = 16;
		string chkCode = string.Empty;
		//颜色列表，用于验证码、噪线、噪点 
		Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
		//字体列表，用于验证码 
		string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
		//验证码的字符集，去掉了一些容易混淆的字符 
		char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
		Random rnd = new Random();
		//生成验证码字符串 
		for (int i = 0; i < 4; i++) {
			chkCode += character[rnd.Next(character.Length)];
		}
        //写入Session 
        if (HttpContext.Current.Session != null)
		   HttpContext.Current.Session["Code"] = chkCode;
        HttpContext.Current.Cache.Insert("vCode", chkCode);
        //南梁验证码
        Code.code = chkCode;
		//创建画布
		Bitmap bmp = new Bitmap(codeW, codeH);
		Graphics g = Graphics.FromImage(bmp);
		g.Clear(Color.White);
		//画噪线 
		for (int i = 0; i < 1; i++) {
			int x1 = rnd.Next(codeW);
			int y1 = rnd.Next(codeH);
			int x2 = rnd.Next(codeW);
			int y2 = rnd.Next(codeH);
			Color clr = color[rnd.Next(color.Length)];
			g.DrawLine(new Pen(clr), x1, y1, x2, y2);
		}
		//画验证码字符串 
		for (int i = 0; i < chkCode.Length; i++) {
			string fnt = font[rnd.Next(font.Length)];
			Font ft = new Font(fnt, fontSize);
			Color clr = color[rnd.Next(color.Length)];
			g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18 + 2, (float)0);
		}
		//画噪点 
		for (int i = 0; i < 100; i++) {
			int x = rnd.Next(bmp.Width);
			int y = rnd.Next(bmp.Height);
			Color clr = color[rnd.Next(color.Length)];
			bmp.SetPixel(x, y, clr);
		}
		MemoryStream ms = new MemoryStream();
		byte[] bytes = null;
		try {
			bmp.Save(ms, ImageFormat.Png);
			bytes = ms.ToArray();
		} finally {
			//显式释放资源 
			bmp.Dispose();
			g.Dispose();
		}

		return bytes;
	}
    public static byte[] ToByte(out string code) {
        int codeW = 80;
        int codeH = 30;
        int fontSize = 16;
        string chkCode = string.Empty;
        //颜色列表，用于验证码、噪线、噪点 
        Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue };
        //字体列表，用于验证码 
        string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" };
        //验证码的字符集，去掉了一些容易混淆的字符 
        char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' };
        Random rnd = new Random();
        //生成验证码字符串 
        for (int i = 0; i < 4; i++) {
            chkCode += character[rnd.Next(character.Length)];
        }
        code = chkCode;
         //创建画布
         Bitmap bmp = new Bitmap(codeW, codeH);
        Graphics g = Graphics.FromImage(bmp);
        g.Clear(Color.White);
        //画噪线 
        for (int i = 0; i < 1; i++) {
            int x1 = rnd.Next(codeW);
            int y1 = rnd.Next(codeH);
            int x2 = rnd.Next(codeW);
            int y2 = rnd.Next(codeH);
            Color clr = color[rnd.Next(color.Length)];
            g.DrawLine(new Pen(clr), x1, y1, x2, y2);
        }
        //画验证码字符串 
        for (int i = 0; i < chkCode.Length; i++) {
            string fnt = font[rnd.Next(font.Length)];
            Font ft = new Font(fnt, fontSize);
            Color clr = color[rnd.Next(color.Length)];
            g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18 + 2, (float)0);
        }
        //画噪点 
        for (int i = 0; i < 100; i++) {
            int x = rnd.Next(bmp.Width);
            int y = rnd.Next(bmp.Height);
            Color clr = color[rnd.Next(color.Length)];
            bmp.SetPixel(x, y, clr);
        }
        MemoryStream ms = new MemoryStream();
        byte[] bytes = null;
        try {
            bmp.Save(ms, ImageFormat.Png);
            bytes = ms.ToArray();
        } finally {
            //显式释放资源 
            bmp.Dispose();
            g.Dispose();
        }

        return bytes;
    }
    public static bool IsVerify(string appId, string appSecret, string token) {
        DxCsharpSDK.Captcha captcha = new DxCsharpSDK.Captcha(appId, appSecret);
        //captcha.SetCaptchaUrl("http://cap.dingxiang-inc.com/api/tokenVerify");
        //特殊情况需要额外指定服务器,可以在这个指定，默认情况下不需要设置
        //captcha.SetTimeOut(2000);
        //设置超时时间，单位毫秒，默认2秒
        DxCsharpSDK.CaptchaResponse response = captcha.VerifyToken(token);
        // Response.Write(response.captchaStatus);
        //确保验证状态是SERVER_SUCCESS，SDK中有容错机制，在网络出现异常的情况会返回通过
        if (response.result) {
            /**token验证通过，继续其他流程**/
            return true;
        } else {
            /**token验证失败，业务系统可以直接阻断该次请求或者继续弹验证码**/
            return false;
        }
    }

    /// <summary>
    /// 南梁验证码校验
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static bool IsCode(string code, string date) {
        string v_code = HttpRuntime.Cache.Get(date).ToString().ToUpper();
        if (code.ToUpper() == v_code)
            return true;
        else
            return false;
    }
}

public class VerifyCodeResult {
    public int response { get; set; }
    public int evil_level { get; set; }
    public string err_msg { get; set; }
}

public class Code {
    public static string code { get; set; }
}